package com.zara.store.client.view;

import java.awt.Color;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Observable;

import com.zara.store.client.clientmodel.PalcItemModel;
import com.zara.store.client.clientmodel.PalcModel;
import com.zara.store.client.controller.PalcController;
import com.zara.store.client.view.swing.PalcForm;
import com.zara.store.client.view.swing.table.PalcTableModel;
import com.zara.store.common.ProductVO;

import framework.clientmodel.Model;
import framework.view.View;

public class PalcView extends View {
	
//	private NumberFormat nf = NumberFormat.getCurrencyInstance(new Locale("es", "AR"));
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private PalcForm palcForm;
	
	public PalcView(Model model) {
		super(model);
		PalcController palcController = new PalcController(model, this);
		setController(palcController);
		palcForm = new PalcForm(this);
	}
	
	public void update(Observable arg0, Object arg1) {
		PalcModel model = (PalcModel) getModel();
		
		//no confundir, es el model de un control swing especifico, estamos actualizando la gui
		PalcTableModel table = palcForm.getPalcTableModel();
		
		int lastIndex = model.getLastIndex();
		//si la rowCownt == model.items.size-1 hay que agregar el ultimo.
		if(lastIndex > 0 && table.getRowCount()==lastIndex) {
			ProductVO lastProduct = model.getLastProduct();
			table.addRow(new Object[]{lastProduct.getBarcode(),lastProduct.getName(),
					"0","","0","0",lastProduct.getStock(), lastProduct.getOrderStock()});
		}
		//el 1er update
		if(table.getRowCount()!=model.getSize()) {
			for(PalcItemModel i: model.getItems()) {
				ProductVO product = i.getProduct();
				String inOfad = i.getAvailableInOfad()? "Disponible":"";
				table.addRow(new Object[]{product.getBarcode(), product.getName(), i.getAmount(), inOfad, 
						i.getAmountSoldLastWeek(), i.getAmountPendingOnLastPalc(), product.getStock(), product.getOrderStock()});
			}
		}
		//clear
		if(model.getSize()<table.getRowCount()) {
			table.clear();
		}
		
		if(model.getPalcCreado()) {
			table.setModifiable(false);
			palcForm.getTablePalc().setBackground(new Color(0xeeeeee));
			
			String content = "<html><body bgcolor=\"ffffff\"><center>" +
			"<h1>ZARA</h1>" +
			"<h2>Entidad</h2>" +
			"<h3>Direcci&oacute;n - Tel&eacute;fono - Web</h3>" +
			"<br>" +
			"<h3>" +
			sdf.format(new Date()) +
			"</h3>" +
			"<br>" +
			"<h2>Pedido a La Coru&ntilde;a</h2>" +
			"" +
			"<table width=\"800\" border=\"1\">" +
			"<tr>" +
			"<th align=\"center\">&nbsp;</th>" +
			"<th align=\"center\">Producto</th>" +
			"<th align=\"center\">Cantidad</th>" +
			"</tr>";
			String content2 = "<tr>" +
			"<td align=\"center\">" +
			"";
			String content2_1 = "</td>" +
			"<td align=\"left\">" +
			"";
			String content2_2 = "</td>" +
			"<td align=\"right\">" +
			"";
			String content2_5 = "</td>" +
			"</tr>";
			String content3 = "</table><br><br></center></body></html>";

			int i = 0;
			for(PalcItemModel l: model.getItems()) {
				BigDecimal amount = l.getAmount();
				if(!BigDecimal.ZERO.equals(amount)) {
					content += content2;
					ProductVO product = l.getProduct();
					content += product.getBarcode();
					content += content2_1;
					content += product.getName();
					content += content2_2;
					content += l.getAmount();
					content += content2_5;
					i++;
				}
			}
			content += content3;
			
			if(i>0) {
				palcForm.getPalcWindow().setContent(content);
				palcForm.getPalcWindow().pack();
				palcForm.getPalcWindow().setVisible(true);
			}
			
		} else {
			table.setModifiable(true);
			palcForm.getTablePalc().setBackground(new Color(0xffffff));
			
		}

	}

	public void display() {
		palcForm.setVisible(true);
	}

	public PalcForm getPalcForm() {
		return palcForm;
	}

};
